package dice;

import com.google.wave.api.*;
import java.util.regex.*;

@SuppressWarnings("serial")
public class DiceServlet extends AbstractRobotServlet {

  @Override
  public void processEvents(RobotMessageBundle bundle) {
    Wavelet wavelet = bundle.getWavelet();
              
    if (bundle.wasSelfAdded()) {
      Blip blip = wavelet.appendBlip();
      TextView textView = blip.getDocument();
      textView.append("Greetings, Heroes! Type: xxdXxx+xx() where 'x' is an optional digit and 'X' is an obligatory digit.");
    }
            
    for (Event e: bundle.getEvents()) {
      if (e.getType() == EventType.BLIP_SUBMITTED) {    
    	Blip blip = e.getBlip();
        generateDiceAndRolls(blip);
      }
    }
  }
  
  public static void generateDiceAndRolls(Blip blip){
	  TextView doc = blip.getDocument();
      String blipContent = doc.getText();
      Pattern pat = Pattern.compile("[0-9]{0,2}[dk]{1,2}[0-9]{1,3}[+]{0,1}[0-9]{0,2}[(][)]");
      Matcher seq = pat.matcher(blipContent);
      String newContent = "";
   	  seq.find();
   	  int beg = seq.start();
   	  int fin = seq.end();
   	  String found = blipContent.substring(beg, fin);
   	  int[] params = returnParametersFromString(found);
   	  String result ="";
   	  if(params[3]==0){
   		  result = "" + diceRoll(params[0], params[1], params[2]);   		  
   	  }
   	  else{
   		  for(int i=0; i<params[0]; i++){
   			result += "" + diceRoll(1, params[1], 0);
   			if(i<(params[0]-1)) { result += ", "; }
   		  }
   	  }
   	  newContent = seq.replaceFirst(found.substring(0, (found.length()-2)) + "(" + result + ")");
   	  doc.replace(newContent);
  }
  
  public static int diceRoll(int howmuch, int dice, int modifier){
	  int result = 0;
	  java.util.Random roll = new java.util.Random();
	  for(int i=0; i<howmuch; i++){
		  result += roll.nextInt(dice) + 1;
	  }
	  result += modifier;
	  return result;
  }
  
  public static int[] returnParametersFromString(String typeOfRoll){
	  int[] arr = new int[4];
	  
	  String found = "";
	  Pattern pat = Pattern.compile("[0-9]{0,2}[dk][dk0-9]");
      Matcher seq = pat.matcher(typeOfRoll);
      while (seq.find()){
    	  int beg = seq.start();
    	  int fin = seq.end()-2;
    	  found = typeOfRoll.substring(beg, fin);
      }
	  if(found.length()==0) { arr[0]=1; }
	  else{ arr[0]=Integer.parseInt(found);	}
	  if(arr[0]==0) { arr[0]=1; }
	  found = "";
	  
	  pat = Pattern.compile("[dk][0-9]{1,3}");
	  seq = pat.matcher(typeOfRoll);
	  while (seq.find()){
		  int beg = seq.start()+1;
		  int fin = seq.end();
		  found = typeOfRoll.substring(beg, fin);
	  }
	  arr[1] = Integer.parseInt(found);
	  found = "";
	  
	  pat = Pattern.compile("[+]{1}[0-9]{0,2}");
	  seq = pat.matcher(typeOfRoll);
	  while (seq.find()){
		  int beg = seq.start()+1;
		  int fin = seq.end();
		  found = typeOfRoll.substring(beg, fin);
	  }
	  if(found.length()==0) { arr[2]=0; }
	  else { arr[2] = Integer.parseInt(found); }
	  found = "";
	  
	  pat = Pattern.compile("(dd)|(kk)");
	  seq = pat.matcher(typeOfRoll);
	  while (seq.find()){
		  int beg = seq.start();
		  int fin = seq.end();
		  found = typeOfRoll.substring(beg, fin);
	  }
	  if(found.length()==0) { arr[3]=0; }
	  else { arr[3] = 1; }
	  found = "";
	  	
	  return arr;
  }
  
}
